<?php
/**
 * Session Class
 * @author solar
 * @version 1.0
 */
class Session
{
	function __construct($autoStart=true) {
    	if($autoStart) $this->start();
		register_shutdown_function(array($this,'close'));
   	}
   	
	public function get($key,$defaultValue=null){
		return isset($_SESSION[$key]) ? $_SESSION[$key] : $defaultValue;
	}

	public function set($key,$value){
		$_SESSION[$key]=$value;
	}

	public function remove($key){
		if(isset($_SESSION[$key])){
			$value=$_SESSION[$key];
			unset($_SESSION[$key]);
			return $value;
		}else return null;
	}
	
	public function start(){
		@session_start();
	}

	public function close(){
		if(session_id()!=='') @session_write_close();
	}

	public function destroy(){
		if(session_id()!==''){
			@session_unset();
			@session_destroy();
		}
	}

	public function getIsStarted(){
		return session_id()!=='';
	}

	public function getSessionID(){
		return session_id();
	}

	public function setSessionID($value){
		session_id($value);
	}

	public function getSessionName(){
		return session_name();
	}

	public function setSessionName($value){
		session_name($value);
	}

	public function getSavePath(){
		return session_save_path();
	}

	public function setSavePath($value){
		if(is_dir($value)) session_save_path($value);		
	}

	public function getCookieParams(){
		return session_get_cookie_params();
	}

	public function setCookieParams($value){
		$data=session_get_cookie_params();
		extract($data);
		extract($value);
		if(isset($httponly))
			session_set_cookie_params($lifetime,$path,$domain,$secure,$httponly);
		else
			session_set_cookie_params($lifetime,$path,$domain,$secure);
	}
	
	public function getTimeout(){
		return (int)ini_get('session.gc_maxlifetime');
	}

	public function setTimeout($value){
		ini_set('session.gc_maxlifetime',$value);
	}	

	public function getCount(){
		return count($_SESSION);
	}

	public function count(){
		return $this->getCount();
	}

	public function getKeys(){
		return array_keys($_SESSION);
	}	

	public function clear(){
		foreach(array_keys($_SESSION) as $key)
			unset($_SESSION[$key]);
	}

	public function contains($key){
		return isset($_SESSION[$key]);
	}

	public function toArray(){
		return $_SESSION;
	}
}
